package com.example.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TextViewActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);

        findViewById(R.id.read).setOnClickListener(this);
        findViewById(R.id.save).setOnClickListener(this);
        findViewById(R.id.camera).setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        boolean isRead = viewId == R.id.read;
        boolean isSave = viewId == R.id.save;
        boolean isCamera = viewId == R.id.camera;

        String fileName = "123.jpg";
        // 获取私有下载目录
        String path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + File.separatorChar + fileName;
        Log.d("zhoutao", path);

        if (isRead) {
            Log.d("zhoutao", "onClick: 读取");
            Bitmap bitmap = null;
            FileInputStream fis = null;

            try {
//                fis = new FileInputStream(path);
//                bitmap = BitmapFactory.decodeStream(fis);
                bitmap = BitmapFactory.decodeFile(path);

                ImageView img = findViewById(R.id.img);
                img.setImageBitmap(bitmap);

            } catch (Exception e) {
//                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

        } else if (isSave) {
            Log.d("zhoutao", "onClick: 保存");
            // 读取资源目录
            Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.a);
            // 把位图对象保存到图片文件夹
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(path);
                b1.compress(Bitmap.CompressFormat.JPEG, 100, fos);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }
}
